{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "9c56823a-0c19-4acf-8b91-6e7537136874",
   "metadata": {},
   "source": [
    "# Agentic RAG"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "a408fc41-460b-422e-9f01-dd63cfa9631a",
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "USER_AGENT environment variable not set, consider setting it to identify your requests.\n"
     ]
    },
    {
     "ename": "ImportError",
     "evalue": "Could not import tiktoken python package. This is needed in order to calculate max_tokens_for_prompt. Please install it with `pip install tiktoken`.",
     "output_type": "error",
     "traceback": [
      "\u001b[31m---------------------------------------------------------------------------\u001b[39m",
      "\u001b[31mModuleNotFoundError\u001b[39m                       Traceback (most recent call last)",
      "\u001b[36mFile \u001b[39m\u001b[32mE:\\pythonShop\\pythonAgent\\Lib\\site-packages\\langchain_text_splitters\\base.py:181\u001b[39m, in \u001b[36mTextSplitter.from_tiktoken_encoder\u001b[39m\u001b[34m(cls, encoding_name, model_name, allowed_special, disallowed_special, **kwargs)\u001b[39m\n\u001b[32m    180\u001b[39m \u001b[38;5;28;01mtry\u001b[39;00m:\n\u001b[32m--> \u001b[39m\u001b[32m181\u001b[39m     \u001b[38;5;28;01mimport\u001b[39;00m\u001b[38;5;250m \u001b[39m\u001b[34;01mtiktoken\u001b[39;00m\n\u001b[32m    182\u001b[39m \u001b[38;5;28;01mexcept\u001b[39;00m \u001b[38;5;167;01mImportError\u001b[39;00m:\n",
      "\u001b[31mModuleNotFoundError\u001b[39m: No module named 'tiktoken'",
      "\nDuring handling of the above exception, another exception occurred:\n",
      "\u001b[31mImportError\u001b[39m                               Traceback (most recent call last)",
      "\u001b[36mCell\u001b[39m\u001b[36m \u001b[39m\u001b[32mIn[1]\u001b[39m\u001b[32m, line 21\u001b[39m\n\u001b[32m     18\u001b[39m docs = [WebBaseLoader(url).load() \u001b[38;5;28;01mfor\u001b[39;00m url \u001b[38;5;129;01min\u001b[39;00m urls]\n\u001b[32m     19\u001b[39m docs_list = [item \u001b[38;5;28;01mfor\u001b[39;00m sublist \u001b[38;5;129;01min\u001b[39;00m docs \u001b[38;5;28;01mfor\u001b[39;00m item \u001b[38;5;129;01min\u001b[39;00m sublist]\n\u001b[32m---> \u001b[39m\u001b[32m21\u001b[39m text_splitter = \u001b[43mRecursiveCharacterTextSplitter\u001b[49m\u001b[43m.\u001b[49m\u001b[43mfrom_tiktoken_encoder\u001b[49m\u001b[43m(\u001b[49m\n\u001b[32m     22\u001b[39m \u001b[43m    \u001b[49m\u001b[43mchunk_size\u001b[49m\u001b[43m=\u001b[49m\u001b[32;43m100\u001b[39;49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mchunk_overlap\u001b[49m\u001b[43m=\u001b[49m\u001b[32;43m50\u001b[39;49m\n\u001b[32m     23\u001b[39m \u001b[43m)\u001b[49m\n\u001b[32m     24\u001b[39m doc_splits = text_splitter.split_documents(docs_list)\n\u001b[32m     26\u001b[39m \u001b[38;5;66;03m# Add to vectorDB\u001b[39;00m\n",
      "\u001b[36mFile \u001b[39m\u001b[32mE:\\pythonShop\\pythonAgent\\Lib\\site-packages\\langchain_text_splitters\\base.py:183\u001b[39m, in \u001b[36mTextSplitter.from_tiktoken_encoder\u001b[39m\u001b[34m(cls, encoding_name, model_name, allowed_special, disallowed_special, **kwargs)\u001b[39m\n\u001b[32m    181\u001b[39m     \u001b[38;5;28;01mimport\u001b[39;00m\u001b[38;5;250m \u001b[39m\u001b[34;01mtiktoken\u001b[39;00m\n\u001b[32m    182\u001b[39m \u001b[38;5;28;01mexcept\u001b[39;00m \u001b[38;5;167;01mImportError\u001b[39;00m:\n\u001b[32m--> \u001b[39m\u001b[32m183\u001b[39m     \u001b[38;5;28;01mraise\u001b[39;00m \u001b[38;5;167;01mImportError\u001b[39;00m(\n\u001b[32m    184\u001b[39m         \u001b[33m\"\u001b[39m\u001b[33mCould not import tiktoken python package. \u001b[39m\u001b[33m\"\u001b[39m\n\u001b[32m    185\u001b[39m         \u001b[33m\"\u001b[39m\u001b[33mThis is needed in order to calculate max_tokens_for_prompt. \u001b[39m\u001b[33m\"\u001b[39m\n\u001b[32m    186\u001b[39m         \u001b[33m\"\u001b[39m\u001b[33mPlease install it with `pip install tiktoken`.\u001b[39m\u001b[33m\"\u001b[39m\n\u001b[32m    187\u001b[39m     )\n\u001b[32m    189\u001b[39m \u001b[38;5;28;01mif\u001b[39;00m model_name \u001b[38;5;129;01mis\u001b[39;00m \u001b[38;5;129;01mnot\u001b[39;00m \u001b[38;5;28;01mNone\u001b[39;00m:\n\u001b[32m    190\u001b[39m     enc = tiktoken.encoding_for_model(model_name)\n",
      "\u001b[31mImportError\u001b[39m: Could not import tiktoken python package. This is needed in order to calculate max_tokens_for_prompt. Please install it with `pip install tiktoken`."
     ]
    }
   ],
   "source": [
    "# pip install langchain_community==0.3.12\n",
    "# pip install chromadb \n",
    "# pip install tiktoken\n",
    "from langchain_community.document_loaders import WebBaseLoader\n",
    "from langchain_community.vectorstores import Chroma\n",
    "\n",
    "from langchain_text_splitters import RecursiveCharacterTextSplitter\n",
    "\n",
    "from chromadb.utils import embedding_functions\n",
    "ollama_ef = embedding_functions.OllamaEmbeddingFunction(\n",
    "                url=\"http://192.168.99.142:11434/api/embeddings\",\n",
    "                model_name=\"nomic-embed-text:latest\"\n",
    ")\n",
    "\n",
    "urls = [\n",
    "    \"https://lilianweng.github.io/posts/2023-06-23-agent/\",\n",
    "]\n",
    "\n",
    "docs = [WebBaseLoader(url).load() for url in urls]\n",
    "docs_list = [item for sublist in docs for item in sublist]\n",
    "\n",
    "text_splitter = RecursiveCharacterTextSplitter.from_tiktoken_encoder(\n",
    "    chunk_size=100, chunk_overlap=50\n",
    ")\n",
    "doc_splits = text_splitter.split_documents(docs_list)\n",
    "\n",
    "# Add to vectorDB\n",
    "vectorstore = Chroma.from_documents(\n",
    "    documents=doc_splits,\n",
    "    collection_name=\"rag-chroma\",\n",
    "    embedding=ollama_ef,\n",
    ")\n",
    "retriever = vectorstore.as_retriever()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "2ff9dd39-5baa-4f44-a91f-0f8b04640d6b",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.13.2"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
